/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.ui;

import commonlib.ui.LoginAdmin;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Administrador
 */
public class LoginAdminView extends LoginAdmin {

    private LoginAdminViewController LAVC;

    private JPanel mainPanel = new JPanel();
    private JPanel passPanel = new JPanel();
    private JPanel buttonPanel = new JPanel();
    private JPanel compPanel = new JPanel();

    private JPasswordField pass = new JPasswordField();
    private JLabel passMess = new JLabel("Password :");
    private JLabel compMess = new JLabel();
    private JButton acc = new JButton("Accept");
    private JButton can = new JButton("Cancel");

    public LoginAdminView(LoginAdminViewController LAVC) {
        this.LAVC = LAVC;
        iniComponents();
    }


    public void actionPerformedPassEdit(){
        String act = String.copyValueOf(pass.getPassword());
        if (LAVC.verificationPass(act))
        {
            compMess.setForeground(new java.awt.Color(51, 153, 0));
            compMess.setText("Correct Password");
            acc.setEnabled(true);
        }
        else
        {
            compMess.setForeground(new java.awt.Color(240, 0, 0));
            compMess.setText("Incorrect Password");
            acc.setEnabled(false);
        }
        this.pack();
        this.repaint();
    }

    public void actionPerformedCancelButtom(){
        LAVC.cancel();
    }

    public void actionPerformedAcceptButtom(){
        LAVC.accept();
    }

    private void iniComponents() {
        iniFrame();
        iniMainPanel();
        assignListeners();
    }

    private void iniFrame(){
        setMinimumSize(new Dimension(280, 150));
        this.setLocationRelativeTo(null);
        this.setPreferredSize(this.getMinimumSize());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setResizable(false);
        JPanel contentPane = (JPanel) this.getContentPane();
        contentPane.add(mainPanel);
    }

    private void iniMainPanel(){
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        acc.setPreferredSize(new Dimension(100, 25));
        buttonPanel.add(acc);
        can.setPreferredSize(new Dimension(100, 25));
        buttonPanel.add(can);

        GroupLayout layout = new GroupLayout(passPanel);
        passPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup heightGroup = layout.createSequentialGroup();
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(passMess));
        heightGroup.addGroup(layout.createParallelGroup()
            .addComponent(pass));
        layout.setHorizontalGroup(heightGroup);

        GroupLayout.SequentialGroup verGroup = layout.createSequentialGroup();
        verGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
                .addComponent(passMess)
                .addComponent(pass));
        layout.setVerticalGroup(verGroup);

        compPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        compPanel.add(compMess);

        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(passPanel, BorderLayout.NORTH);
        mainPanel.add(compPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void assignListeners(){
        pass.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
            public void removeUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
            public void changedUpdate(DocumentEvent e) {
                actionPerformedPassEdit();
            }
        });

        acc.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedAcceptButtom();
            }
        });

        can.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedCancelButtom();
            }
        });
    }

    protected void AcceptarMouseClicked(MouseEvent evt){
    }

    protected void RebutjarMouseClicked(MouseEvent evt){
    }

    protected void AcceptarErrorMouseClicked(MouseEvent evt){
    }

}
